﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Oracle.DataAccess.Client;
using System.Data;
using System.Configuration;

namespace Testify
{
    public partial class Enrollment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnShowInsert_onClick(object sender, EventArgs e)
        {
            this.gdvStudents.FooterRow.Visible = true;
        }

        protected void btnCancelInsert_onClick(object sender, EventArgs e)
        {
            this.gdvStudents.FooterRow.Visible = false;
        }

        protected void btnInsert_onClick(object sender, EventArgs e)
        {
            
        }

        public void btnEnrollStudents_Click(object sender, EventArgs e)
        {
           this.lblErrorMsg.Text = String.Empty;

           if (this.ddlClasses.Items.Count == 0)
           {
              this.lblErrorMsg.Text += "Please Select a class/section.<br />";
           }
           else
           {
              string queryString1 = @"SELECT student_id 
                                        FROM section
                                        JOIN enrollment using (section_id)
                                        JOIN class using (class_id)
                                        JOIN term using (term_id)
                                       WHERE term_end_date > sysdate
                                         AND term_start_date < add_months(sysdate, 6)
                                         AND class_id = (SELECT distinct class_id 
                                                           FROM section
                                                           --JOIN enrollment USING (section_id)
                                                          WHERE section_id = :section_id
                                                            AND section_maint <> 'D')
                                         AND student_id = :student_id
                                         AND section_maint <> 'D'";

              OracleConnection myConnection1 = new OracleConnection(ConfigurationManager.ConnectionStrings["Singularity"].ConnectionString);
              OracleCommand cmdEnroll = new OracleCommand("ENROLLMENTS.addEnrollment", myConnection1);
              cmdEnroll.CommandType = CommandType.StoredProcedure;
              cmdEnroll.BindByName = true;

              OracleCommand myCommand1 = new OracleCommand(queryString1, myConnection1);
              myCommand1.BindByName = true;
              OracleDataReader myReader1 = default(OracleDataReader);
              OracleTransaction myTransaction1 = null;
              bool enrollStudent = false;

              try
              {
                 myConnection1.Open();

                 myTransaction1 = myConnection1.BeginTransaction();
                 cmdEnroll.Transaction = myTransaction1;
                 myCommand1.Transaction = myTransaction1;
                 bool firstRepeatError = true;
                 foreach (GridViewRow r in this.gdvStudents.Rows)
                 {

                    if((r.FindControl("chkAdd") as CheckBox).Checked)
                    {
                          myCommand1.Parameters.Clear();
                          myCommand1.Parameters.Add("student_id", (r.FindControl("lblStudentID") as Label).Text);
                          myCommand1.Parameters.Add("section_id", ddlClasses.SelectedValue);
                          myReader1 = myCommand1.ExecuteReader();
                          if (myReader1.HasRows)
                          {
                             enrollStudent = false;
                             if (firstRepeatError)
                             {
                                firstRepeatError = false;
                                this.lblErrorMsg.Text += "The following students could not be added because they are already in the class.<br />";
                             }
                             //string[] name = ((r.FindControl("lblStudentName") as Label).Text.Split(','));
                             this.lblErrorMsg.Text += "&nbsp;&nbsp;&nbsp;" + (r.FindControl("lblStudentName") as Label).Text + "<br />";
                          }
                          else
                          {
                             enrollStudent = true;
                          }

                       if (enrollStudent)
                       {
                          cmdEnroll.Parameters.Clear();
                          cmdEnroll.Parameters.Add("p_AdminID", Session["UserID"].ToString());
                          cmdEnroll.Parameters.Add("p_StudentID", (r.FindControl("lblStudentID") as Label).Text);
                          cmdEnroll.Parameters.Add("p_SectionID", ddlClasses.SelectedValue);
                          cmdEnroll.ExecuteNonQuery();
                       }
                    }
                 }

                 myTransaction1.Commit();
                 if (String.IsNullOrEmpty(this.lblErrorMsg.Text))
                 {
                    this.lblInfoMsg.Text = "Students Enrolled.<br />";
                 }
                 else
                 {
                    this.lblInfoMsg.Text = "Students Enrolled (with errors).<br />";
                 }
                 this.gdvStudents.DataBind();
              }
              catch (Exception ex)
              {
                 myTransaction1.Rollback();

                 this.lblErrorMsg.Text += "ERROR Enrolling students.<br />";
                 Utilities.logError(Page.Title, ex);
              
              }
              finally
              {
                 myConnection1.Close();
              }
           }
        }

        protected void showAddEnroll_Click(object sender, EventArgs e)
        {
           if (this.ddlClasses.SelectedIndex != 0)
           {
              this.pnlEnroll.Visible = true;
           }
           this.lbtAddEnroll.Visible  = false;

           this.pnlViewEnroll.Visible = false;
           this.lbtViewEnroll.Visible = true;

           this.lblErrorMsg.Text = String.Empty;
           this.lblInfoMsg.Text = String.Empty;
        }

        protected void changeDisplay_Click(object sender, EventArgs e)
        {
           this.pnlEnroll.Visible    = false;
           this.lbtAddEnroll.Visible = true;

           this.pnlViewEnroll.Visible = true;
           this.lbtViewEnroll.Visible = false;

           this.gdvViewEnroll.DataBind();
           this.lblErrorMsg.Text = String.Empty;
           this.lblInfoMsg.Text = String.Empty;
        }

        protected void ddlClasses_SelectedIndexChanged(object sender, EventArgs e)
        {
           if (this.lbtViewEnroll.Visible)
           {
              if (this.ddlClasses.SelectedIndex == 0)
              {
                 this.pnlEnroll.Visible = false;
              }
              else
              {
                 this.pnlEnroll.Visible = true;
              }
           }
        }

        protected void clearDropDown(object sender, EventArgs e)
        {
           this.ddlClasses.Items.Clear();
           this.ddlClasses.Items.Add(new ListItem("Select a Class", "-1"));
        }

        protected void ddlClasses_DataBound(object sender, EventArgs e)
        {
            this.gdvViewEnroll.DataBind();
            this.pnlEnroll.Visible = false;
        }

    }
}